<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
if( $action == 'addcomment' && !empty($_POST) ) {
	$data_array = $_POST;
	$data_array = util_htmlfilter($data_array);
	$data_array['field_content'] = util_substr($data_array['field_content'], 0, 4000);
	$target = $data_array['target'];
	$type = $data_array['form'];
	
	//
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/comment/comment.class.php');
	$mObj = new CommentBase();
	$mObj->setTarget($target);
	
	if( $type == 'content_comment' ) {
		$type = COMMENT_TYPE_CONTENT;
	} elseif( $type == 'news_comment' ) {
		$type = COMMENT_TYPE_NEWS;
	} elseif( $type == 'page_comment' ) {
		$type = COMMENT_TYPE_PAGE;
	} elseif( $type == 'product_comment' ) {
		$type = COMMENT_TYPE_PRODUCT;
	}
	$mObj->setType($type);
	
	//
	$mObj->setcachetime('hour');
	$mObj->cacheclean('getItems', array('', 0, true));
	$items_id = $mObj->addItem($data_array);
	
	//
	$r = array();
	$r['f'] = $f;
	if( $items_id > 0 ) {
		$r['status'] = 1;	
	} else {
		$r['status'] = 0;	
	}
	
	//
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'lib.json.class.php');
	echo JsonUtil::encode($r);
	exit();
} elseif( $action == 'listcomment' ) {
	$target = trim(strtolower(util_get_param($_REQUEST, 'target')));
	$type = trim(strtolower(util_get_param($_REQUEST, 'type')));
	//
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/comment/comment.class.php');
	$mObj = new CommentBase();
	$mObj->setTarget($target);
	
	if( $type == 'content_comment' ) {
		$type = COMMENT_TYPE_CONTENT;
	} elseif( $type == 'news_comment' ) {
		$type = COMMENT_TYPE_NEWS;
	} elseif( $type == 'page_comment' ) {
		$type = COMMENT_TYPE_PAGE;
	} elseif( $type == 'product_comment' ) {
		$type = COMMENT_TYPE_PRODUCT;
	}
	$mObj->setType($type);
	
	//
	$limit_total = $mObj->getItemsTotal();
	$mObj->setcachetime('hour');
	$items = $mObj->cache('getItems', array('', 0, true));
	if( is_array($items) ) {
		$items = util_stripslashes($items);
	}
	
	//
	$r = array();
	$r['total'] = $limit_total;
	$r['items'] = $items;
	
	//
	//
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'lib.json.class.php');
	echo JsonUtil::encode($r);
	exit();
}
//
?>